package com.example.demo08;

import java.lang.reflect.Field;

import org.json.JSONObject;

import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.edroid.common.utils.HttpUtils;


/**
 * 收集机型信息
 * 
 * @author yichou 2015-12-27
 *
 */
public class BuildUpload {

	public static void upload(Context ctx) {
		String url = "http://edroid.aliapp.com/apkhooker/collect.php?action=addBuild";
		String ret = HttpUtils.post(url, "json=" + get(ctx));
		
		System.out.println(ret);
	}
	
	public static String get(Context ctx) {
		try {
			JSONObject jsonObject = new JSONObject();
			
			Field[] fields = Build.class.getDeclaredFields();
			for(Field f : fields) {
				f.setAccessible(true);
				
				if("SERIAL".equals(f.getName())) continue;
				if("HOST".equals(f.getName())) continue;
				
				if(f.getGenericType() == String.class) {
					jsonObject.put(f.getName(), f.get(null));
				}
			}
			
			DisplayMetrics dm = ctx.getResources().getDisplayMetrics();
			jsonObject.put("WIDTH", dm.widthPixels);
			jsonObject.put("HEIGHT", dm.heightPixels);
			jsonObject.put("DPI", dm.densityDpi);
			
			fields = Build.VERSION.class.getDeclaredFields();
			for(Field f : fields) {
				f.setAccessible(true);
				if(f.getGenericType() == String.class) {
					jsonObject.put(f.getName(), f.get(null));
				}
			}
			
			return (jsonObject.toString());
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
